{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>.container { width:100% !important; }</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.core.display import display, HTML\n",
    "display(HTML(\"<style>.container { width:100% !important; }</style>\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Installation\n",
    "Python Numpy Scipy Theano    \n",
    "Options: h5py Matplotlib Ipython notebook (Jupyter Notebook)  NVIDIA driver, CUDA, cuDNN    \n",
    "Package Manager: pip Windows: Anaconda, Winpython ... Linux: apt-get/dnf/yum\n",
    "\n",
    "```\n",
    "sudo pip install theano\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python\n",
    "### Shallow Copy vs Deep Copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "[1, 2, 0]\n",
      "[1, 2, 0]\n"
     ]
    }
   ],
   "source": [
    "a=[1,2,3]\n",
    "print a\n",
    "b=a\n",
    "b[2]=0\n",
    "print b\n",
    "print a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### don't write sth like len=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']\n",
      "<type 'builtin_function_or_method'>\n"
     ]
    }
   ],
   "source": [
    "a=[1,2,3]\n",
    "print len(a)\n",
    "print dir(a)\n",
    "print type(len)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### List and Dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<type 'list'> [1, 2, 3]\n",
      "<type 'dict'> {'a': 1, 'c': 3, 'b': 2}\n"
     ]
    }
   ],
   "source": [
    "a=[1,2,3]\n",
    "b={'a':1,'b':2,'c':3}\n",
    "print type(a), a\n",
    "print type(b), b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Slice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 4, 5]\n",
      "[4, 5]\n"
     ]
    }
   ],
   "source": [
    "a=[1,2,3,4,5]\n",
    "print a[2:]\n",
    "print a[-2:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Numpy\n",
    "### Index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 3 5]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import numpy.random as RNG\n",
    "a=np.asarray([1,2,3,4,5,6])\n",
    "b=np.asarray([0,2,4])\n",
    "print a[b]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.  1.]\n",
      " [ 1.  1.]\n",
      " [ 1.  1.]]\n",
      "-------\n",
      "[[ 1.  1.  1.]\n",
      " [ 1.  1.  1.]]\n",
      "-------\n",
      "[[ 2.  2.  2.]\n",
      " [ 2.  2.  2.]\n",
      " [ 2.  2.  2.]]\n"
     ]
    }
   ],
   "source": [
    "a=np.ones((3,2))\n",
    "b=np.ones((2,3))\n",
    "print a\n",
    "print '-------'\n",
    "print b\n",
    "print '-------'\n",
    "print np.dot(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Broadcast"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.  2.]\n",
      " [ 2.  2.]\n",
      " [ 2.  2.]]\n"
     ]
    }
   ],
   "source": [
    "a=np.ones((3,2))\n",
    "b=np.ones((3,1))\n",
    "print a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "a=np.ones((1,))\n",
    "b=np.ones((1,1))\n",
    "c=np.ones((1,1,1))\n",
    "print a.ndim\n",
    "print b.ndim\n",
    "print c.ndim"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### axis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3.  3.]\n",
      "[ 2.  2.  2.]\n"
     ]
    }
   ],
   "source": [
    "a=np.ones((3,2))\n",
    "print a.sum(axis=0)\n",
    "print a.sum(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Theano\n",
    "### TensorVariable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'theano.tensor.var.TensorVariable'> <class 'theano.tensor.var.TensorVariable'>\n"
     ]
    }
   ],
   "source": [
    "import theano\n",
    "import theano.tensor as T\n",
    "\n",
    "a=T.matrix()\n",
    "b=T.vector()\n",
    "print type(a), type(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### SharedVariable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'theano.tensor.sharedvar.TensorSharedVariable'>\n"
     ]
    }
   ],
   "source": [
    "a=theano.shared(np.ones((3,2)))\n",
    "print type(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Expression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'theano.tensor.var.TensorVariable'>\n"
     ]
    }
   ],
   "source": [
    "a=T.vector()\n",
    "b=T.vector()\n",
    "c=a+b\n",
    "print type(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Real example, Linear Regression\n",
    "$$X=\\{x_0, x_1, x_2, ..., x_N\\} x_i\\in R $$\n",
    "$$Y=\\{y_0, y_1, y_2, ..., y_N\\} y_i\\in R $$\n",
    "$$W\\in R, b\\in R $$\n",
    "$$\\hat y = Wx+b $$\n",
    "$$Loss = \\frac{1}{2}\\sum(\\hat Y-Y)^2 $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#define inputs\n",
    "x=T.vector()\n",
    "y=T.vector( )\n",
    "\n",
    "#define parameters\n",
    "W=theano.shared(RNG.random())\n",
    "b=theano.shared(NP.array(0.0))\n",
    "params = [W,b]\n",
    "\n",
    "#build an equation \n",
    "y_hat = x*W+b\n",
    "\n",
    "#define a loss\n",
    "loss = T.mean(0.5*(y_hat-y)**2)\n",
    "\n",
    "#calculate the gradient \n",
    "grads = theano.grad(loss, params)\n",
    "\n",
    "#gradient descent \n",
    "param_update = []\n",
    "for p,g in zip(params, grads):\n",
    "    param_update.append((p, p-lr*g))\n",
    "    \n",
    "#compile the theano function\n",
    "train_func = theano.function([x, y], [loss, y_hat], updates=param_update, allow_input_downcast=True)\n",
    "print 'COMPILE DONE'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### THEANO_FLAGS\n",
    "```\n",
    "    THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32,profile=True\n",
    "    THEANO_FLAGS=mode=FAST_COMPILE,device=cpu,floatX=float64\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
